package com.cy.store.service.impl;

import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.pojo.Cart;
import com.cy.store.pojo.Product;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import com.cy.store.vo.CartVo;
import com.sun.org.apache.bcel.internal.generic.IFEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @PROJECT_NAME: store
 * @DESCRIPTION:
 * @USER: 28416
 * @DATE: 2022/11/8 16:17
 */
@Service
public class CartServiceImpl  implements ICartService {
    /**
     * 购物车的业务层依赖于购物车的持久层以及商品的持久层
     */
    @Autowired
    private  CartMapper cartMapper;
    @Autowired
    private  ProductMapper productMapper;

    /**
     * 把某个商品加入到购物车
     * @param uid
     * @param pid
     * @param count
     * @param username
     */
    @Override
    public void addToCart(Integer uid, Integer pid, Integer count, String username) {
        //执行插入操作之前
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        if (result == null) {//表示这个商品从来没有被添加在购物车内
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setPid(pid);
            cart.setNum(count);
            cart.setModifiedUser(username);
            cart.setModifiedTime(new Date());
            cart.setCreatedTime(new Date());
            cart.setCreatedUser(username);
            //价格 来至于商品中的数据  补全价格
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());
            Integer insert = cartMapper.insert(cart);
            if (insert == null||insert !=1) {
                throw new InsertException("插入时发生未知的异常");
            }
        }else{//表示当前的商品在购物车中，则更新这条数据的num值

            Integer integer = cartMapper.updateNumerByCid(result.getCid(), count+result.getNum(), username, new Date());
            if (integer != 1) {
                throw new UpdateException("更新数据时发生了未知的异常");
            }
        }

    }

    /**
     *查找uid的收货地址
     * @param uid
     * @return
     */
    @Override
    public List<CartVo> getVOByUid(Integer uid) {
        return cartMapper.findByUid(uid);
    }

    /**
     * 增加购物车里的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer updateNumByCid(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()+1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }

    /**
     * 减少购物车的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()-1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }

    /**
     * 结算页面查询购物车里被选中的商品
     * @param uid
     * @param cids
     * @return
     */
    @Override
    public List<CartVo> getVOByCid(Integer uid, Integer[] cids) {
        List<CartVo> list = cartMapper.findVOByCid(cids);
        Iterator<CartVo> iterator = list.iterator();
        while (iterator.hasNext()) {
            CartVo next = iterator.next();
            if (!next.getUid().equals(uid)){
                list.remove(next);
            }
        }
        return list;
    }
}
